package gov.decea.projects.sgq.controllers;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import gov.decea.projects.sgq.api.MainService;
import gov.decea.projects.sgq.core.exception.CoreException;
import gov.decea.projects.sgq.forms.MetarGroupDataFormBean;
import gov.decea.projects.sgq.forms.TafGroupDataFormBean;
import gov.decea.projects.sgq.model.MetarGroupData;
import gov.decea.projects.sgq.model.TafGroupData;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.WebRequest;

/**
 *
 * @author dctp
 */
@Controller
@RequestMapping("/meteorological_reports")
public class MeteorologicalReportsController {

    private final MainService mainService;

    @Autowired
    public MeteorologicalReportsController(MainService mainService) {
        this.mainService = mainService;
    }

    @RequestMapping({"/", "/index", ""})
    public String index(Model model, WebRequest request) {

        List<Map<Object, Object>> dataList = new ArrayList<Map<Object, Object>>();

        List<MetarGroupData> dataGroupList = mainService.findAllMetar(new ArrayList<Long>(), null, null);

        for (MetarGroupData dataGroup : dataGroupList) {
            Map<Object, Object> item = new HashMap<Object, Object>();
            item.put("id", dataGroup.getId());
            item.put("totalMetar", dataGroup.getTotalMetar());
            item.put("totalSpeci", dataGroup.getTotalSpeci());
            item.put("totalMetarAtrasado", dataGroup.getTotalMetarAtrasado());
            item.put("totalSpeciAtrasado", dataGroup.getTotalSpeciAtrasado());
            dataList.add(item);
        }

        model.addAttribute("total", dataList.size());
        model.addAttribute("dataList", dataList);

        return "meteorological_reports";
    }

    @RequestMapping("/new_metar_group")
    public String newMetarGroup(Model model, WebRequest request) {
        model.addAttribute(new MetarGroupDataFormBean());
        return "metar_group_data/form";
    }

    @RequestMapping("/new_taf_group")
    public String newTafGroup(Model model, WebRequest request) {
        model.addAttribute(new TafGroupDataFormBean());
        return "taf_group_data/form";
    }

    @RequestMapping("/save_taf_group")
    public String saveTafGroup(Model model, @Valid TafGroupDataFormBean form,
            BindingResult bindingResult, WebRequest request) {

        SimpleDateFormat df = new SimpleDateFormat("mm/yyyy");
        Date periodo = null;
        try {
            periodo = df.parse(form.getMes());
        } catch (ParseException ex) {
            Logger.getLogger(MeteorologicalReportsController.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (periodo == null) {
            form.setMes(null);
        }

        if (bindingResult.hasErrors()) {
            return "taf_group_data/form";
        }

        TafGroupData groupData = new TafGroupData();
        groupData.setPeriodo(periodo);
        groupData.setTotalAtrasados(form.getTotalAtrasados());
        groupData.setTotalAusentes(form.getTotalAusentes());
        groupData.setTotalEmendas(form.getTotalEmendas());
        groupData.setTotalPrevisto(form.getTotalPrevisto());

        mainService.createMeteorologicalReportGroupData(groupData);

        return "redirect:index";
    }

    @RequestMapping("/save_metar_group")
    public String saveMetarGroup(Model model, @Valid MetarGroupDataFormBean form,
            BindingResult bindingResult, WebRequest request) {
        SimpleDateFormat df = new SimpleDateFormat("mm/yyyy");
        Date periodo = null;
        try {
            periodo = df.parse(form.getMes());
        } catch (ParseException ex) {
            Logger.getLogger(MeteorologicalReportsController.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (periodo == null) {
            form.setMes(null);
        }

        if (bindingResult.hasErrors()) {
            return "metar_group_data/form";
        }

        MetarGroupData groupData = new MetarGroupData();
        groupData.setPeriodo(periodo);
        groupData.setTotalMetar(form.getTotalMetar());
        groupData.setTotalSpeci(form.getTotalSpeci());
        groupData.setTotalLOCAL(form.getTotalLOCAL());
        groupData.setTotalMetarAtrasado(form.getTotalMetarAtrasado());
        groupData.setTotalMetarAusente(form.getTotalMetarAusente());
        groupData.setTotalSpeciAtrasado(form.getTotalSpeciAtrasado());

        mainService.createMeteorologicalReportGroupData(groupData);

        return "redirect:index";
    }
}
